package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.system.api.utils.DictionaryUtils;
import com.jcfk.system.domain.dto.SysNoticeDTO;
import com.jcfk.system.domain.po.SysNotice;
import com.jcfk.system.service.SysDictionaryItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jcfk.system.domain.query.SysNoticeQuery;
import com.jcfk.system.service.SysNoticeService;

import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;


/**
 * @author awei
 * @email 522891237@gqq.com
 * @date 2023-11-24 09:54:15
 */
@Api(tags = {"管理"})
@RestController
@RequestMapping("sysnotice")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;

    @Autowired
    private SysDictionaryItemService sysDictionaryItemService;
    @Autowired
    private DictionaryUtils dictionaryUtils;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysNoticeQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询")
    @PostMapping("/queryByPage")
    @ResponseBody
    public RetPage<SysNoticeDTO> queryByPage(@RequestBody ReqPageParam<SysNoticeQuery> requestParam) {
        DataPage<SysNoticeDTO> page = new DataPage<SysNoticeDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), new OrderItem("create_time", false), SysNoticeDTO.class);
        //DataPage<SysNoticeDTO> page = new DataPage<SysNoticeDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), SysNoticeDTO.class);
        page = sysNoticeService.queryByPage(page, requestParam.getData());

        dictionaryUtils.bindDictionaryLabel(page.getRecords(), "NoticeType", "noticeType", "noticeTypeLable");
        return new RetPage<>(page);
    }


    /**
     * 根据id 查询详情
     */
    @ApiOperation("查询")
    @GetMapping("/get/{id}")
    @RequestLogHeader(modular = "管理", businessType = "详情")
    @ResponseBody
    public RetResult<SysNoticeDTO> info(@PathVariable("id") Long id) {
        SysNoticeDTO item = sysNoticeService.get(id);
        dictionaryUtils.bindDictionaryLabel(item, "NoticeType", "noticeType", "noticeTypeLable");
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    @ResponseBody
    @RequestLogHeader(modular = "管理", businessType = "保存")
    public RetResult<SysNoticeDTO> save(@RequestBody SysNoticeDTO sysNotice) {
        // 数据校验
        RetResult ret = sysNoticeService.isValid(sysNotice);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysNotice.getId())) {
            // 新增
            return RetResult.ok(sysNoticeService.insert(sysNotice));
        } else {
            // 更新
            return RetResult.ok(sysNoticeService.update(sysNotice));
        }

    }


    /**
     * 发布
     */
    @ApiOperation("发布")
    @PostMapping("/send")
    @ResponseBody
    @RequestLogHeader(modular = "管理", businessType = "保存")
    public RetResult<?> send(@RequestBody SysNoticeDTO sysNotice) {
        RetResult<SysNoticeDTO> saveResult = this.save(sysNotice);
        SysNoticeDTO data = saveResult.getData();
        sysNoticeService.send(data);
        return RetResult.ok();
    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除")
    @ResponseBody
    @PostMapping("/delete")
    @RequestLogHeader(modular = "管理", businessType = "批量删除")
    public RetResult<?> deleteList(@RequestBody SysNoticeDTO sysNotice) {
        return RetResult.ok(sysNoticeService.logicDeleteList(sysNotice));
    }

    /**
     * 获取指定数量公告
     *
     * @param count
     * @param count
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "int", name = "count", value = "", required = true)})
    @ApiOperation(value = "获取指定数量公告", notes = "获取指定数量公告", httpMethod = "POST")
    @PostMapping("getList/{count}")
    public RetResult<List<SysNoticeDTO>> queryTake(@PathVariable("count") int count) {
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper();
        queryWrapper.last("LIMIT " + count).orderByDesc("create_time");
        List<SysNotice> list = sysNoticeService.list(queryWrapper);

        return RetResult.ok(convertList(list, SysNoticeDTO.class));
    }


}
